package cn.good.yan.sf.t7;


/**
 * 7 队列： 基于双栈结构 , 使用栈模拟 队列的数据结构
 * (双栈模拟队列)
 *
 * @author shengren.yan
 * @create 2024-07-27
 */
public class StackToQueue<E> {

    // head
    // tail  3 2 1
    LinkListStack<E> head = new LinkListStack<>(100);
    LinkListStack<E> tail = new LinkListStack<>(100);
    int size = 0;

    // 向队列添加元素
    public boolean offer(E e) {
        tail.push(e);
        size++;
        return true;
    }

    // 向队列移除元素
    public E pool() {
        if (head.isEmpty()) {
            if (!tail.isEmpty()) {
                head.push(tail.pol());
            }
        }
        size--;
        return head.pol();
    }

    // 从队列头获取值，并不移除
    public E peek() {
        if (head.isEmpty()) {
            while (!tail.isEmpty()) {
                E pol = tail.pol();
                head.push(pol);
            }
        }
        return head.peek();
    }

    // 检查队列是否为空，true就是空
    public boolean isEmpty() {
        return head.isEmpty() && tail.isEmpty();
    }

    // 检查队列是否满了，true就是满了
    public boolean isFull() {
        return size == 100;
    }

    public static void main(String[] args) {
        StackToQueue<Integer> q = new StackToQueue<>();
        System.out.println("移除元素：" + q.pool());
        System.out.println("获取头元素：" + q.peek());
        System.out.println("添加元素：" + q.offer(1));
        System.out.println("添加元素：" + q.offer(2));
        System.out.println("添加元素：" + q.offer(3));
        System.out.println("添加元素：" + q.offer(4));
        System.out.println("添加元素：" + q.offer(5));
        System.out.println("添加元素：" + q.offer(6));
        System.out.println("添加元素：" + q.offer(7));
        System.out.println("--------------");
        System.out.println("获取头元素：" + q.peek());
        System.out.println("获取头元素：" + q.peek());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("--------------");
        System.out.println("--------------");
    }

}
